#!/home/work/rewrite/usr/bin/python
import sys
from time import strptime, mktime

DEBUG = True

def process_oq(stats, anum, sid):
    xmlDoc = ''
    pos = 0

    if DEBUG:
        print '\n'.join(['\t'.join(stat) for stat in stats]) + '\n'

    oqDatas = []
    xmlDocs = []

    for stat in stats:
        (spos, query, clickcnt, sclickcnt, stimestr, etimestr, actionType) = stat
        actionType = int(actionType)
        if pos > 0 and  actionType != -1:
            ostat = stats[pos-1]
            (ospos, oquery, oclickcnt, osclickcnt, ostimestr, oetimestr, oactionType) = ostat

            oqData  = '\t'.join(ostat) + '\n'
            oqData += '\t'.join(stat) + '\n'
            #oqDatas.append(oqData)

            if DEBUG:
                print oqData
            
            spos = int(spos)
            ospos = int(ospos)

            start = int(mktime(strptime(stimestr, '%d/%b/%Y:%H:%M:%S')))
            ostart = int(mktime(strptime(ostimestr, '%d/%b/%Y:%H:%M:%S')))
            
            end = int(mktime(strptime(etimestr, '%d/%b/%Y:%H:%M:%S')))
            oend = int(mktime(strptime(oetimestr, '%d/%b/%Y:%H:%M:%S')))


            step = start - oend
            if step < 0:
                step = 0 - step

            query = query.replace("&", "&amp;").replace('"', "&quot;").replace("<", "&lt;").replace(">", "&gt;")
            oquery = oquery.replace("&", "&amp;").replace('"', "&quot;").replace("<", "&lt;").replace(">", "&gt;")
            
            xmlDoc  = '<sphinx:document id="%s">\n' % spos
            xmlDoc += '<hsid>%s</hsid>\n' % (sid >> 32)
            xmlDoc += '<lsid>%s</lsid>\n' % (sid & 0xffffffff)
            xmlDoc += '<type>%s</type>\n' % actionType
            xmlDoc += '<oquery><![CDATA[%s]]></oquery>\n' % oquery
            xmlDoc += '<query><![CDATA[%s]]></query>\n' % query
            xmlDoc += '<opos>%s</opos>\n' % (ospos - anum + 1)
            xmlDoc += '<pos>%s</pos>\n' % (spos - anum + 1)
            xmlDoc += '<oclick>%s</oclick>\n' % oclickcnt
            xmlDoc += '<click>%s</click>\n' % clickcnt
            xmlDoc += '<osatisfy>%s</osatisfy>\n' % osclickcnt
            xmlDoc += '<satisfy>%s</satisfy>\n' % sclickcnt
            xmlDoc += '<timestep>%s</timestep>\n' % step
            xmlDoc += '<ostart>%s</ostart>\n' % ostart
            xmlDoc += '<start>%s</start>\n' % start
            xmlDoc += '<oend>%s</oend>\n' % oend
            xmlDoc += '<end>%s</end>\n' % end
            xmlDoc += '</sphinx:document>'

            if DEBUG:
                xmlDoc += '\n'
            
            #xmlDocs.append(xmlDoc)
            print xmlDoc

        pos += 1
    #return (oqDatas, xmlDocs)


def process_session(actions, snum, anum, cmd):
    actioncnt = len(actions)
    if actioncnt < 0:
        return
   
    if cmd == 2 or DEBUG:
        sid = actions[0][0]
        sData  = '\t'.join( ( sid, str(anum), str(actioncnt) ) ) + '\n'
        pos = anum
        for cols in actions:
            if DEBUG:
                sData += '\t'.join([str(pos)] + cols[1:]) + '\n'
            else:
                sData += '\t'.join(cols[1:]) + '\n'

            pos += 1

        if DEBUG:
            print sData

    queryDict = {}
    queries = []
    querystat = []
    
    stimestr = ''
    etimestr = ''
    actionType = ''
    spos = 1
    pos = 0
    clickcnt = 0
    sclickcnt = 0
    (osid, otimestr, ostype, oquery, ourl, ohit, ohtype) = (None, None, None, None, None, None, None)
    for action in actions:
        (sid, timestr, stype, query, url, hit, htype) = action

        if htype == 'se' and query != oquery:
            etimestr = otimestr

            if oquery != None:
                querystat.append([str(spos), oquery, str(clickcnt), str(sclickcnt), stimestr, etimestr, actionType])

            clickcnt = 0
            sclickcnt = 0
            
            stimestr = timestr
            actionType = stype
            spos = pos + anum

        if (htype == 'as' or htype.find('al') >-1) and query == oquery:
            clickcnt += 1

            if hit == '1':
                sclickcnt += 1
         
        if not queryDict.has_key(query):
            queryDict[query] = 1
            queries.append(query)
        else:
            queryDict[query] += 1

        (osid, otimestr, ostype, oquery, ourl, ohit, ohtype) = action

        pos += 1
    
    querystat.append([str(spos), oquery, str(clickcnt), str(sclickcnt), stimestr, etimestr, actionType])
        
    querycnt = len(queries)
    querytxt = '\n'.join(queries).replace("&", "&amp;").replace('"', "&quot;").replace("<", "&lt;").replace(">", "&gt;")
    
    if DEBUG or cmd == 0:
        sxmlDoc  = '<sphinx:document id="%s">\n' % snum
        sxmlDoc += '<queries>\n<![CDATA[\n%s\n]]>\n</queries>\n' % querytxt
        sxmlDoc += '<actioncnt>%s</actioncnt>\n' % actioncnt
        sxmlDoc += '<querycnt>%s</querycnt>\n' % querycnt
        sxmlDoc += '</sphinx:document>'
        if DEBUG:
            sxmlDoc += '\n'

        print sxmlDoc

    if DEBUG or cmd == 1:
        process_oq(querystat, anum, snum)

def session_xmldoc(path, datenum, fileno, cmd):
    lastsid = ''
    actions = []

    snum = ((datenum * 300 + fileno) << 32) + 1
    anum = ((datenum * 300 + fileno) << 32) + 1

    sxmlschema  = '<?xml version="1.0" encoding="utf-8"?>\n'
    sxmlschema += '<sphinx:docset>\n'
    sxmlschema += '<sphinx:schema>\n'
    sxmlschema += '<sphinx:field name="queries"/>\n'
    sxmlschema += '<sphinx:attr name="actioncnt" type="int"/>\n'
    sxmlschema += '<sphinx:attr name="querycnt" type="int"/>\n'
    sxmlschema += '</sphinx:schema>'
    
    oqxmlschema  = '<?xml version="1.0" encoding="utf-8"?>\n'
    oqxmlschema += '<sphinx:docset>\n'
    oqxmlschema += '<sphinx:schema>\n'
    oqxmlschema += '<sphinx:field name="oquery"/>\n'
    oqxmlschema += '<sphinx:field name="query"/>\n'
    oqxmlschema += '<sphinx:attr name="hsid" type="int"/>\n'
    oqxmlschema += '<sphinx:attr name="lsid" type="int"/>\n'
    oqxmlschema += '<sphinx:attr name="type" type="int"/>\n'
    oqxmlschema += '<sphinx:attr name="opos" type="int"/>\n'
    oqxmlschema += '<sphinx:attr name="pos" type="int"/>\n'
    oqxmlschema += '<sphinx:attr name="oclick" type="int"/>\n'
    oqxmlschema += '<sphinx:attr name="click" type="int"/>\n'
    oqxmlschema += '<sphinx:attr name="osatisfy" type="int"/>\n'
    oqxmlschema += '<sphinx:attr name="satisfy" type="int"/>\n'
    oqxmlschema += '<sphinx:attr name="timestep" type="int"/>\n'
    oqxmlschema += '<sphinx:attr name="ostart" type="timestamp"/>\n'
    oqxmlschema += '<sphinx:attr name="start" type="timestamp"/>\n'
    oqxmlschema += '<sphinx:attr name="oend" type="timestamp"/>\n'
    oqxmlschema += '<sphinx:attr name="end" type="timestamp"/>\n'
    oqxmlschema += '</sphinx:schema>'
    
    if not DEBUG:
        if cmd == 0:
            print sxmlschema
        elif cmd == 1:
            print oqxmlschema

    for line in file(path):
        line = line.strip()
        if not line:
            continue

        line = unicode(line, 'gb18030', 'ignore').encode('utf-8', 'ignore')
        cols = line.split('\t')
        if len(cols) != 7:
            continue

        (sid, timestr, stype, query, url, hit, htype) = cols
        if sid != lastsid:
            actioncnt = len(actions)
            if len(actions) > 0:
                process_session(actions, snum, anum, cmd)
                #print snum, anum
                snum +=1
                anum += actioncnt
                actions = []

            lastsid = sid

        actions.append(cols)

    process_session(actions, snum, anum, cmd)
    #print snum, anum

    if not DEBUG and cmd != 2:
        print '</sphinx:docset>'

if __name__ == '__main__':
    import sys
    
    if len(sys.argv) < 4:
        print 'Usage: %s path datenum fileno cmd' % (sys.argv[0])
        sys.exit()
    
    path = sys.argv[1]
    dateno = int(sys.argv[2])
    fileno = int(sys.argv[3])
    cmd = int(sys.argv[4]) # 0 1 2

    session_xmldoc(path, dateno, fileno, cmd)
